{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "150d9e6d",
   "metadata": {},
   "source": [
    "(git-repo-as-hub)=\n",
    "# Using a Git repo as a hub\n",
    "\n",
    "You can save functions in a Git repo, and use this repo as your own hub.\n",
    "This repo structure must conform with:\n",
    "- The name of the function YAML must be named `function.yaml`. You can use the {py:class}`~mlrun.runtimes.BaseRuntime.export`\n",
    "method to create the function yaml file.\n",
    "- The .yaml file must stored in a path like this: /function-name/function.yaml (e.g /func/function.yaml), for example: \n",
    "`https://raw.githubusercontent.com/user-name/repo-name/function-name/function.yaml`.\n",
    "- If you have additional files, for example a source file or a notebook example, they can be stored in the same folder as the function.yaml.\n",
    "\n",
    "```{admonition} Tip\n",
    "You can use Git tags for function versioning in Git. For example, to import a function named func that has a v1 tag:\n",
    "\n",
    "`import_func_1 = mlrun.import_function('hub://func:v1')`\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15d0bcf6",
   "metadata": {},
   "source": [
    "## Create and export an MLRun function from a file\n",
    "\n",
    "You can use the function tag to tag the function in MLRun. It is not related to the Git tag. For example, this \n",
    "function has a 'version1' tag in MLRun and a 'v1' tag in Git."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13a08e5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "function = project.set_function(\n",
    "    name=\"func-hub\",\n",
    "    tag=\"version1\",\n",
    "    handler=\"func\",\n",
    "    image=\"mlrun/mlrun\",\n",
    "    func=\"./my-hub/func/func.py\",\n",
    "    kind=\"job\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a239575",
   "metadata": {},
   "source": [
    "Export the function to a YAML file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a387151",
   "metadata": {},
   "outputs": [],
   "source": [
    "function.export(\"./my-hub/func/function.yaml\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e774941",
   "metadata": {},
   "source": [
    "## Import and run the function from your repo \n",
    "\n",
    "You can use a import function from your \"Git repo function hub\" by pointing to it with its full URL, for example:\n",
    " `https://raw.githubusercontent.com/user-name/repo-name/tag/name/function.yaml`.\n",
    "\n",
    "```{admonition} Working with tags\n",
    "Assume there are multiple versions in Git: v1, v2, etc. You specify which version you want by appending `:tag#` to \n",
    "the hub path. The path must be to a folder that contains the `function.yaml` file in the func directory. \n",
    "```\n",
    "```{admonition} Private repo\n",
    " If working from a private repo, set:<br>\n",
    "`project.set_secret({\"HTTPS_AUTH_TOKEN\":<Http-Token, e.g. GIT-TOKEN})`\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14cd8049",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the v1 tag from git:\n",
    "import_func_1 = project.set_function(\n",
    "    \"https://raw.githubusercontent.com/user-name/repo-name/v1/func/function.yaml\",\n",
    "    name=\"<function-name>\",\n",
    ")\n",
    "\n",
    "# print the results\n",
    "print(import_func_1.to_yaml())\n",
    "\n",
    "# Run the function:\n",
    "import_func_1.run()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
